#pragma once
#include "common.h"
#include <sys/epoll.h>

class Reactor
{
private:
  int _epfd;

public:
  Reactor() : _epfd(-1)
  {
    InitReator();
  }
  ~Reactor()
  {
    close(_epfd);
  }
  int waitUtilEvReady(epoll_event *events, int size, timeval *tv)
  {
    // tv指针为空则阻塞等待 ， 否则等待时间转换为毫秒
    int nums_events = epoll_wait(_epfd, events, size, tv ? tv->tv_sec * 1000 + tv->tv_usec / 1000 : -1);
    return nums_events;
  }
  bool modifyEv(int fd, int op, epoll_event *events)
  {
    int ret = epoll_ctl(_epfd, op, fd, events);
    if (ret == -1)
      return false;
    else
      return true;
  }

private:
  void InitReator()
  {
    _epfd = epoll_create(128);
    if (_epfd < 0)
    {
      cerr << "epoll create error" << endl;
      exit(-1);
    }
  }
};
